package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.framework.AccountManager;
import com.shaoshaohuo.app.framework.ActivityCollector;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.net.RequestParam;
import com.shaoshaohuo.app.utils.CircleImageView;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.shaoshaohuo.app.utils.im.IMFactory;
import com.squareup.picasso.Picasso;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import me.nereo.multi_image_selector.MultiImageSelectorActivity;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/6/7.
 */

public class BianJi extends BaseActivity implements View.OnClickListener {

    private ImageView imageview_topbar_left_backImage;
    private Intent intent;
    private RelativeLayout activity_bj_photo;
    private CircleImageView ac_bj_photo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bj);
        View viewById = findViewById(R.id.banji_rela);
        changeTransltestaus(this, viewById, TransltestausModel.NoePicter, getResources().getColor(R.color.color_49b152));
        //获取数据
        intent = getIntent();
        TextView toolbar_topname = (TextView) findViewById(R.id.toolbar_topname);
        toolbar_topname.setText("我的账号");
        imageview_topbar_left_backImage = (ImageView) findViewById(R.id.imageview_topbar_left_backImage);
        imageview_topbar_left_backImage.setImageResource(R.mipmap.fanhui_title);
        imageview_topbar_left_backImage.setVisibility(View.VISIBLE);
        ImageView imageview_message = (ImageView) findViewById(R.id.imageview_message);
        imageview_message.setVisibility(View.GONE);
        init();
        viewlistener();
    }



    private void init() {
        //头像地址
        String imaphto = intent.getStringExtra("imaphto");
        //电话号码
        String phonenum = intent.getStringExtra("phonenum");
        TextView ac_bj_tv_mobile = (TextView) findViewById(R.id.ac_bj_tv_mobile);
        ac_bj_tv_mobile.setText(phonenum);
        ac_bj_photo = (CircleImageView) findViewById(R.id.ac_bj_photo);
        if (!TextUtils.isEmpty(imaphto)) {
            Picasso.with(this).load(imaphto).placeholder(R.mipmap.zhanzhanzhan).into(ac_bj_photo);
        }
        Button activity_bj_out = (Button) findViewById(R.id.activity_bj_out);
        activity_bj_out.setOnClickListener(this);
        activity_bj_photo = (RelativeLayout) findViewById(R.id.activity_bj_photo);

    }

    private void viewlistener() {
        imageview_topbar_left_backImage.setOnClickListener(this);
        activity_bj_photo.setOnClickListener(this);
    }

    private ArrayList<String> pathlist = new ArrayList<>();
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageview_topbar_left_backImage:
                finish();
                break;
            case R.id.activity_bj_out:
                AccountManager.logout(BianJi.this);
                SharedPreferencesHelper.setBoolean(getApplicationContext(),
                        SharedPreferencesHelper.Field.IS_COME_BACK, false);
                ActivityCollector.finishAll();
                Intent intent = new Intent(BianJi.this, LoginActivity.class);
                intent.putExtra(LoginActivity.TAG_IS_CAN_NOT_BACK, true);
                startActivity(intent);
                IMFactory.logout();
                MobclickAgent.onProfileSignOff();
                break;
            case R.id.activity_bj_photo:
                Intent intent1 = new Intent();
                tackPic(pathlist, intent1, 0, 1);
                intent1.setClass(this, MultiImageSelectorActivity.class);
                startActivityForResult(intent1, 0);
                break;
        }


    }


    public static void tackPic(ArrayList<String> mSelectPath, Intent intent2, int selectedMode, int maxNum) {
//        int selectedMode = MultiImageSelectorActivity.MODE_MULTI;
//        int maxNum = 10;
//        Intent intent2 = new Intent(mContext, MultiImageSelectorActivity.class);
        // 是否显示拍摄图片
        intent2.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
        // 最大可选择图片数量
        intent2.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, maxNum);
        // 选择模式
        intent2.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, selectedMode);
        // 默认选择
        if (mSelectPath != null && mSelectPath.size() > 0) {
            intent2.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 0:
                    startLoadingDialog();
                    ArrayList<String> select_result = data.getStringArrayListExtra("select_result");
                    String s = select_result.get(0);
                    Log.e("TAG", "onActivityResult: ");
                    File file = new File(s);
                    Map<String, String> headerParams = RequestParam.getHeaderParams();
                    Okhttputils.Instanse(this).upImge(HttpConfig.BASE_HOST_URL + "/upload/header", headerParams, null, "file", file, new Okhttputils.OkutListener() {
                        @Override
                        public void error(Call call, IOException e) {
                             dismissLoadingDialog();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    ToastUtil.toast("失败");
                                }
                            });
                        }

                        @Override
                        public void error(Call call, int code) {
                            dismissLoadingDialog();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    ToastUtil.toast("失败");
                                }
                            });
                        }

                        @Override
                        public void suseff(Call call, Response response, final Object o) {
                            dismissLoadingDialog();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    ToastUtil.toast("成功");
                                    UserHeadEntity userHeadEntity= (UserHeadEntity) o;
                                    if(userHeadEntity.getData().size()>0){
                                        String s1 = userHeadEntity.getData().get(0);
                                        Picasso.with(BianJi.this).load(s1).placeholder(R.mipmap.zhanzhanzhan).into(ac_bj_photo);
                                    }
                                }
                            });
                        }
                    }, UserHeadEntity.class);
//                    if (file.exists()) {
//                        ToastUtil.toast("啊啊啊");
//                    }
                    break;
            }
        }
    }
}
